package com.dhp.contoller;

import com.dhp.Common.ErrorCode;
import com.dhp.Common.Result;
import com.dhp.Common.ResultUtils;
import com.dhp.Exception.exception;
import com.dhp.domain.User;
import com.dhp.domain.request.TeamJoinRequest;
import com.dhp.domain.request.UserLoginRequest;
import com.dhp.domain.request.UserRegisterRequest;
import com.dhp.service.UserService;
import com.dhp.utils.OssUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private OssUtil ossUtil;
    /**
     * 用户登录
     * @param userLoginRequest
     * @param request
     * @return 登录用户的脱敏信息
     */
    @PostMapping("/login")
    public Result<User> userLogin(@RequestBody UserLoginRequest userLoginRequest , HttpServletRequest request , HttpServletResponse response){
        log.info("user logging");
        if (userLoginRequest == null){
            return ResultUtils.error(ErrorCode.PARAMS_EMPTY);
        }

        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount , userPassword)){
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }

        User user =  userService.userLogin(userAccount , userPassword , request , response);
        return ResultUtils.success(user);
    }

    /**
     * 获取当前用户
     * @param request
     * @return
     */
    @GetMapping("/current")
    public Result<User> getCurrentUser(HttpServletRequest request){
        log.info("getting currentUser");
        User user = userService.getCurrentUser(request);
        return ResultUtils.success(user);
    }

    @GetMapping("/recommend")
    public Result<Page<User>> getRecommendUsers(@RequestParam("pageNum") int pageNum , @RequestParam("pageSize") int pageSize , HttpServletRequest request){
        return ResultUtils.success(userService.getRecommendUsers(pageNum , pageSize , request));
    }
    /**
     * 更新用户信息
     * @param updatedUser
     * @return
     */
    @PostMapping("/update")
    public Result<Integer> updateUser(@RequestBody User updatedUser){
        Integer res = userService.updateUser(updatedUser);
        return ResultUtils.success(res);
    }


    /**
     * 按标签搜寻用户
     * @return
     */
    @GetMapping("/search/tags")
    public Result<List<User>> searchUser(@RequestParam(value = "tagNameList",required = false)List<String> tagNameList , @RequestParam(value = "searchText",required = false) String searchText){
        List<User> users = userService.searchUserByTags(tagNameList , searchText);
        return ResultUtils.success(users);
    }

    @GetMapping("/match")
    public Result<List<User>> getMatchUsers(@RequestParam("num") Integer num , HttpServletRequest request){
        List<User> matchUserlist = userService.getMatchUsers(num , request);
        return ResultUtils.success(matchUserlist);
    }

    @PostMapping("/register")
    public Result<User> userRegister(@RequestBody UserRegisterRequest registerRequest , HttpServletRequest request){
        if (registerRequest == null){
            return ResultUtils.error(ErrorCode.PARAMS_EMPTY);
        }

        Long id = userService.userRegister(registerRequest, request);
        return ResultUtils.success(id);
    }

    @PostMapping("/upload")
    public Result<String> uploadFile(MultipartFile file) throws Exception {
        String url = ossUtil.uploadFile(file);
        if (StringUtils.isBlank(url)){
            throw new exception(ErrorCode.BUSINESS_ERROR , "上传失败");
        }
        return ResultUtils.success(url);
    }
}
